// pages/index/index.js
const app = getApp();

Page({
  data: {
    userInfo: {},
    balance: 1000.00,
    recentRecords: [
      {
        type: '斗地主',
        time: '2024-01-20 19:30',
        amount: 150.00,
        isPositive: true
      },
      {
        type: '充值',
        time: '2024-01-20 18:00',
        amount: 500.00,
        isPositive: true
      },
      {
        type: '麻将',
        time: '2024-01-20 16:45',
        amount: 80.00,
        isPositive: false
      }
    ]
  },

  onLoad: function() {
    // 检查登录状态
    if (!app.globalData.userInfo || app.globalData.userInfo === null) {
      wx.navigateTo({ url: '/pages/login/login' });
      return;
    }
    
    // 获取用户信息
    this.setData({
      userInfo: app.globalData.userInfo || { nickname: '', avatar: '', id: '' }
    });
    
    // 这里可以添加加载余额和记录的逻辑
    this.loadUserBalance();
    this.loadRecentRecords();
  },

  onShow: function() {
    // 每次页面显示时刷新数据
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo
      });
      this.loadUserBalance();
      this.loadRecentRecords();
    }
  },

  // 加载用户余额
  loadUserBalance: function() {
    // 模拟从服务器获取余额
    // 实际项目中应该调用API
    const balance = wx.getStorageSync('userBalance') || 1000.00;
    this.setData({ balance: balance });
  },

  // 加载最近记录
  loadRecentRecords: function() {
    // 模拟从服务器获取记录
    // 实际项目中应该调用API
    const records = wx.getStorageSync('recentRecords') || this.data.recentRecords;
    this.setData({ recentRecords: records });
  },

  // 导航到创建房间
  navigateToCreateRoom: function() {
    wx.navigateTo({ url: '/pages/create-room/create-room' });
  },

  // 导航到加入房间
  navigateToJoinRoom: function() {
    wx.navigateTo({ url: '/pages/join-room/join-room' });
  },

  // 导航到记账页面
  navigateToRecord: function() {
    wx.navigateTo({ url: '/pages/record/record' });
  },

  // 导航到转账页面
  navigateToTransfer: function() {
    wx.navigateTo({ url: '/pages/transactions/transactions?type=transfer' });
  },

  // 导航到统计页面
  navigateToStatistics: function() {
    wx.switchTab({ url: '/pages/statistics/statistics' });
  },

  // 导航到排名页面
  navigateToRankings: function() {
    wx.switchTab({ url: '/pages/ranking/ranking' });
  },

  // 导航到交易记录页面
  navigateToTransactions: function() {
    wx.navigateTo({ url: '/pages/transactions/transactions?type=recharge' });
  }
});
